import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:im_module/common/FriendTypeState.dart';
import 'package:im_module/global_context.dart';
import 'package:im_module/info/UserInfoWidget.dart';
import 'package:im_module/manager/ImChatManager.dart';
import 'package:im_module/model/ImUserInfoBean.dart';
import 'package:im_module/provider/im_provider.dart';
import 'package:im_module/widget/AppBarBackground.dart';
import 'package:im_module/widget/ImHeadImageWidget.dart';
import 'package:provider/provider.dart';

typedef void QueryCallback(int type, result);

class SearchFriendWidget extends StatefulWidget {
  final QueryCallback ageCallBack;

  SearchFriendWidget(this.ageCallBack);

  @override
  _SearchFriendWidgetState createState() => _SearchFriendWidgetState();
}

class _SearchFriendWidgetState extends State<SearchFriendWidget> {
  TextEditingController _controller = TextEditingController();

  static bool _delOff = true; //是否展示删除按钮

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();

    Provider.of<ImProvider>(GlobalContext.getContext(), listen: false)
        .clearQueryUserInfoBean();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        FocusScope.of(context).requestFocus(FocusNode());
      },
      child: AppBarBackground(
        child: Scaffold(
          appBar: AppBar(
            title: Text("搜索好友"),
            centerTitle: true,
          ),
          body: Container(
            color: Colors.white,
            child: Padding(
              padding: const EdgeInsets.symmetric(vertical: 10),
              child: Column(
                children: [
                  SizedBox(
                    height: 10,
                  ),
                  Container(
                    margin: EdgeInsets.symmetric(horizontal: 20),
                    height: 40,
                    decoration: new BoxDecoration(
                      color: Color(0xfff7f7f7),
                      borderRadius: BorderRadius.all(Radius.circular(5.0)),
                      shape: BoxShape.rectangle,
                      border: Border.all(color: Color(0xfff7f7f7), width: .5),
                    ),
                    alignment: Alignment.center,
                    child: TextField(
                      controller: _controller,
                      textInputAction: TextInputAction.search,
                      decoration: InputDecoration(
                        prefixIcon: Icon(Icons.search),
                        border: InputBorder.none,
                        hintText: "通过用户名/手机号搜索添加",
                        isDense: true,
                        hintStyle:
                            TextStyle(fontSize: 14, color: Color(0xffb3b3b3)),
                        suffixIcon: GestureDetector(
                          child: Offstage(
                            offstage: _delOff,
                            child: Icon(
                              Icons.highlight_off,
                              color: Colors.grey,
                            ),
                          ),
                          onTap: () {
                            FocusScope.of(context).requestFocus(FocusNode());
                            Provider.of<ImProvider>(context, listen: false)
                                .setQueryUserInfoBean(null);
                          },
                        ),
                      ),
                      maxLines: 1,
                      //keyboardType: TextInputType.number,
                      inputFormatters: <TextInputFormatter>[
                        LengthLimitingTextInputFormatter(11) //限制长度
                      ],

                      onEditingComplete: () {
                        var value = _controller.text;
                        if (value.isEmpty) {
                          print("输入内容不能为空");
                          return;
                        }
                        widget.ageCallBack(1, value);
                      },
                      onChanged: (value) {
                        Provider.of<ImProvider>(context, listen: false)
                            .setQueryUserInfoBean(null);
                      },
                    ),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Consumer<ImProvider>(
                      builder: (context, ImProvider provider, child) {
                    ImUserInfoBean? userInfoBean = provider.userInfoBean;
                    if (userInfoBean == null) {
                      return Expanded(
                        child: Center(
                          child: Text(
                            "暂无数据",
                            style: TextStyle(
                                fontSize: 14, color: Color(0xff999999)),
                          ),
                        ),
                      );
                    }
                    return Container(
                      height: 48,
                      alignment: Alignment.center,
                      padding: EdgeInsets.symmetric(horizontal: 15),
                      child: InkWell(
                        onTap: () {
                          Navigator.push(context,
                              MaterialPageRoute(builder: (_) {
                            return UserInfoWidget(
                                "${userInfoBean.userId}", widget.ageCallBack);
                          }));
                        },
                        child: Row(
                          children: [
                            ImHeadImageWidget(
                              "${userInfoBean.faceurl}",
                              width: 28,
                              height: 28,
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              "${userInfoBean.niceName}",
                              style: TextStyle(
                                  fontSize: 16, color: Color(0xff333333)),
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              "${userInfoBean.phoneNumber}",
                              style: TextStyle(
                                  fontSize: 14, color: Color(0xff999999)),
                            ),
                          ],
                        ),
                      ),
                    );

                    /*return FutureBuilder<int>(
                      future:
                          ImChatManager().checkFriend("${userInfoBean.userId}"),
                      initialData: -1,
                      builder:
                          (BuildContext context, AsyncSnapshot<int> snapshot) {
                        if (snapshot.hasData) {
                          int value = snapshot.data ?? -1;
                          return Container(
                            height: 48,
                            alignment: Alignment.center,
                            padding: EdgeInsets.symmetric(horizontal: 15),
                            child: Row(
                              //mainAxisAlignment: MainAxisAlignment.center,
                              //crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                ImHeadImageWidget(
                                  "${userInfoBean.faceurl}",
                                  width: 28,
                                  height: 28,
                                ),
                                SizedBox(
                                  width: 10,
                                ),
                                Text(
                                  "${userInfoBean.userName}",
                                  style: TextStyle(
                                      fontSize: 16, color: Color(0xff333333)),
                                ),
                                SizedBox(
                                  width: 10,
                                ),
                                Text(
                                  "${userInfoBean.phoneNumber}",
                                  style: TextStyle(
                                      fontSize: 14, color: Color(0xff999999)),
                                ),
                                Expanded(
                                    child: Container(
                                  alignment: Alignment.centerRight,
                                  child: ElevatedButton(
                                    onPressed: () {
                                      provider.clearQueryUserInfoBean();
                                      if (value == 3) {
                                        Navigator.push(context,
                                            MaterialPageRoute(builder: (_) {
                                          return UserInfoWidget(
                                              "${userInfoBean.userId}",
                                              widget.ageCallBack);
                                        }));
                                      } else {
                                        ImChatManager().addFriend(
                                            "${userInfoBean.userId}");
                                        Navigator.pop(context);
                                      }
                                    },
                                    child: Text(value ==
                                            FriendTypeState
                                                .V2TIM_FRIEND_RELATION_TYPE_BOTH_WAY
                                        ? "查看"
                                        : "添加"),
                                  ),
                                ))
                              ],
                            ),
                          );
                        } else {
                          return CupertinoActivityIndicator();
                        }
                      },
                    );*/
                  }),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
